package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.UserRelevance;
import com.sc.nft.enums.UserEcologyHatchRecordsStatus;
import com.sc.nft.mapper.UserRelevanceMapper;
import com.sc.nft.pool.UserPool;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class UserRelevanceDao extends ServiceImpl<UserRelevanceMapper, UserRelevance> {

    public Integer getUserFans() {
        QueryWrapper<UserRelevance> wrapper = new QueryWrapper<>();
        wrapper.eq("lv", 1);
        wrapper.eq("user_id", UserPool.getUser().getId());
        return count(wrapper);
    }
    /**
     * 获取最近的服务中心
     *
     * @param userId
     * @return
     */
    public List<Long> getNearCityServer(Long userId) {
        return baseMapper.getNearCityServer(userId);
    }

    /**
     * 获取最近的工作室
     *
     * @param userId
     * @return
     */
    public List<Long> getNearWorkRoom(Long userId) {
        return baseMapper.getNearWorkRoom(userId);
    }

    public List<Long> userRelevanceByUserId(Long userId) {
        return baseMapper.userRelevanceByUserId(userId);
    }

    public List<Long> relevanceIdByUserIdList(List<Long> userIdList) {
        return baseMapper.userRelevanceByUserIdList(userIdList);
    }

    public List<UserRelevance> getByRelevance (Long userId ) {
        LambdaQueryWrapper<UserRelevance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRelevance::getRelevanceId, userId);
        wrapper.orderByAsc(UserRelevance::getLv);
        return list(wrapper);
    }


    public Integer getByUserIdAndLvAndNotInRelevanceIdCount(Long userId, String start) {
        Integer count = baseMapper.getByUserIdAndLvAndNotInRelevanceIdCount(userId, start);
        return count;
    }


    public List<UserRelevance> getByUserIdAndLv(Long userId, int lv) {
        LambdaQueryWrapper<UserRelevance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRelevance::getUserId, userId);
        wrapper.eq(UserRelevance::getLv, lv);
        return list(wrapper);
    }

    public List<UserRelevance> ListByUserIdAndRelevanceIdAndLv(Long userId,List<Long> allRelevanceId, int lv) {
        LambdaQueryWrapper<UserRelevance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRelevance::getUserId, userId);
        wrapper.in(UserRelevance::getRelevanceId, allRelevanceId);
        wrapper.le(UserRelevance::getLv, lv);
        wrapper.orderByAsc(UserRelevance::getLv);
        return list(wrapper);
    }

    public UserRelevance getByRelevanceIdAndLv(Long relevanceId, int lv) {
        LambdaQueryWrapper<UserRelevance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRelevance::getRelevanceId, relevanceId);
        wrapper.eq(UserRelevance::getLv, lv);
        return getOne(wrapper);
    }


    /**
     * 获取用户自己当前的旗下用户,且排除名下的社区网点,实际拥有的人员ID
     *
     * @param userId
     * @param info
     * @return
     */
    public List<Long> excludeSubordinateCommunity(Long userId, List<Long> info, List<Long> excludesTheCityServiceCenterOfTheCurrentUser) {
        return baseMapper.excludeSubordinateCommunity(userId, info, excludesTheCityServiceCenterOfTheCurrentUser);
    }

    /**
     * 获取用户自己当前的旗下用户,且排除名下的社区网点,伞下lv级别实际拥有的人员ID
     *
     * @param userId
     * @param info
     * @return
     */
    public List<Long> excludeSubordinateCommunityWithLv(Long userId, List<Long> info, List<Long> excludesTheCityServiceCenterOfTheCurrentUser,Integer lv) {
        return baseMapper.excludeSubordinateCommunityWithLv(userId, info, excludesTheCityServiceCenterOfTheCurrentUser,lv);
    }



    public List<Long> excludeCityServiceCentersWithLv(Long userId, List<Long> info,Integer lv){
        return baseMapper.excludeCityServiceCentersWithLv(userId,info,lv);
    }


    public List<Long> excludeCityServiceCenters(Long userId, List<Long> info) {
        return baseMapper.excludeCityServiceCenters(userId, info);
    }

    public Boolean shaolinTempleContractDisputes(Long userId, Long relevanceId) {
        LambdaQueryWrapper<UserRelevance> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(UserRelevance::getUserId, userId);
        lambdaQuery.eq(UserRelevance::getRelevanceId, relevanceId);
        return count(lambdaQuery) > 0;
    }


    public List<Long> excludesTheCityServiceCenterOfTheCurrentUser(Long userId) {
        return baseMapper.excludesTheCityServiceCenterOfTheCurrentUser(userId);
    }

    /**
     * 获取某个用户是否在某个人的网体下
     *
     * @param userId
     * @param relevanceId
     * @return
     */
    public UserRelevance subordinate(Long userId, Long relevanceId) {
        LambdaQueryWrapper<UserRelevance> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(UserRelevance::getUserId, userId);
        lambdaQuery.eq(UserRelevance::getRelevanceId, relevanceId);
        return getOne(lambdaQuery);
    }

    /**
     * 获取直推数量
     * @param userId
     * @return
     */
    public List<UserRelevance> directThrustCount(Long userId) {
        Wrapper<UserRelevance> wrapper = new QueryWrapper<UserRelevance>().eq("user_id", userId).eq("lv", 1);
        return list(wrapper);
    }
    /**
     * 获取直推社区网点数量
     * @param userId
     * @return
     */
    public Integer directThrustCommunityNetworkCount(Long userId) {
        return baseMapper.directThrustCommunityNetworkCount(userId);
    }
    /**
     * 获取直推城市服务中心数量
     * @param userId
     * @return
     */
    public Integer directThrustUrbanServiceCenterCount(Long userId) {
        return baseMapper.directThrustUrbanServiceCenterCount(userId);
    }

    public Boolean getIsExistByUserIdAndRelevanceId(Long userId, Long relevanceId) {
        LambdaQueryWrapper<UserRelevance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRelevance::getUserId, userId);
        wrapper.eq(UserRelevance::getRelevanceId, relevanceId);
        return count(wrapper) >0;
    }
    /**
     * 下级必须是这个用户网体内的,并且必须要是没被截断的
     * @param userId
     * @param relevanceId
     * @return
     */
    public Boolean verifyThatTheUserIsInTheirOwnNetwork(Long userId, Long relevanceId) {
        return SqlHelper.retBool(baseMapper.verifyThatTheUserIsInTheirOwnNetwork(userId, relevanceId));
    }

    /**
     * 获取最近直接服务中心
     * @param userId
     * @return
     */
    public Long getNearDirectCityServer(Long userId) {
        return baseMapper.getNearDirectCityServer(userId);
    }

    /**
     * 获取最近直接社区网点
     * @param userId
     * @return
     */
    public Long getNearDirectWorkRoom(Long userId) {
        return baseMapper.getNearDirectWorkRoom(userId);
    }

    public List<UserRelevance> ListByUserIdAndRelevanceIdAndLvAndDayCollection(Long participationSmeltUserId,List<Long> participationSmeltUserIds, int day, int lv, Long collectionId, UserEcologyHatchRecordsStatus status) {
        if (day == 365){
            return baseMapper.ListByUserIdAndRelevanceIdAndLvAndDayCollection(participationSmeltUserId,participationSmeltUserIds,day,lv,collectionId,status.getCode());
        }else {
            DateTime beginOfWeek = DateUtil.beginOfWeek(DateUtil.lastWeek());
            DateTime endOfWeek = DateUtil.endOfWeek(DateUtil.lastWeek());
            return baseMapper.ListByUserIdAndRelevanceIdAndLvAndWeekCollection(participationSmeltUserId,participationSmeltUserIds,beginOfWeek,endOfWeek,lv,collectionId,status.getCode());
        }

    }
}
